/**
 * window.localStorage
 * @method set 设置
 * @method get 获取
 * @method remove 移除
 * @method clear 移除全部
 */
export const Local = {
    set(key: string, val: any) {
        window.localStorage.setItem(key, JSON.stringify(val))
    },
    get(key: string) {
        let json: any = window.localStorage.getItem(key)
        return JSON.parse(json)
    },
    remove(key: string) {
        window.localStorage.removeItem(key)
    },
    clear() {
        window.localStorage.clear()
    },
}

/**
 * window.sessionStorage
 * @method set 设置会话缓存
 * @method get 获取会话缓存
 * @method remove 移除会话缓存
 * @method clear 移除全部会话缓存
 */
export const Session = {
    set(key: string, val: any) {
        window.sessionStorage.setItem(key, JSON.stringify(val))
    },
    get(key: string) {
        let json: any = window.sessionStorage.getItem(key)
        return JSON.parse(json)
    },
    remove(key: string) {
        window.sessionStorage.removeItem(key)
    },
    clear() {
        window.sessionStorage.clear()
    },
}
/**
 * cookie
 */
export const Cookie = {
    /**
     *  设置cookie
     * @param cname
     * @param cvalue
     * @param exdays
     */
    setCookie(cname: string, cvalue: any, exdays = 720) {
        var d = new Date();
        d.setTime(d.getTime() + exdays * 24 * 60 * 60 * 1000);
        var expires = 'expires=' + d.toUTCString();
        document.cookie = cname + '=' + cvalue + '; ' + expires;
    },
    getCookie(cname: string) {
        var name = cname + '=';
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ') c = c.substring(1);
            if (c.indexOf(name) != -1) {
                return c.substring(name.length, c.length);
            }
        }
        return '';
    },
    removeCookie(cname: string) {
        var d = new Date();
        d.setTime(-1);
        var expires = 'expires=' + d.toUTCString();
        document.cookie = cname + "=''; " + expires;
    }
}
